package com.zzyl.mapper;

import com.github.pagehelper.Page;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface DeviceDataMapper {
    int deleteByPrimaryKey(Long id);

    int insert(DeviceData record);

    int insertSelective(DeviceData record);

    DeviceData selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(DeviceData record);

    int updateByPrimaryKey(DeviceData record);

    /**
     * 这是Mybatis Generator拓展插件生成的方法(请勿删除).
     * This method corresponds to the database table device_data
     *
     * @mbg.generated
     * @author hewei
     */
    int batchInsert(@Param("list") List<DeviceData> list);

    /**
     * 物模型分页查询
     * @param deviceDataPageReqDto
     * @return
     */
    Page<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto);

    /**
     * 按天统计查询指标数据
     * @param iotId
     * @param functionId
     * @param minCreateTime
     * @param maxCreateTime
     * @return
     */
    List<DeviceDataGraphVo> queryDeviceDataListByDay(
            @Param("iotId") String iotId, @Param("functionId") String functionId,
            @Param("minCreateTime") LocalDateTime minCreateTime,
            @Param("maxCreateTime") LocalDateTime maxCreateTime);

    /**
     * 按周统计查询指标数据
     * @param iotId
     * @param functionId
     * @param minCreateTime
     * @param maxCreateTime
     * @return
     */
    List<DeviceDataGraphVo> queryDeviceDataListByWeek(@Param("iotId") String iotId,
                                                      @Param("functionId") String functionId,
                                                      @Param("minCreateTime") LocalDateTime minCreateTime,
                                                      @Param("maxCreateTime") LocalDateTime maxCreateTime);

    /**
     * 定时任务, 只保存一个月的设备数据
     */
    @Delete("DELETE FROM device_data WHERE alarm_time > DATE_SUB(NOW(),INTERVAL 30 day")
    void deleteData();
}